package org.mountainpunch.ds.sparsematrix.test;

import org.mountainpunch.ds.sparsematrix.MatrixDataOperable;
import org.mountainpunch.ds.sparsematrix.SparseMatrix;
import org.mountainpunch.ds.sparsematrix.SparseMatrixIterator;

/**
 *
 * @author siddharth
 */
public class SparseMatrixAssertion {

  public static boolean NDEBUG = false;

  private static void printStack(String why) {
    Throwable t = new Throwable(why);
    t.printStackTrace();
    System.exit(1);
  }

  private static void assertMatrix(boolean expression, String why) {
    if(NDEBUG && !expression) {
      printStack(why);
    }
  }

  public static <T extends MatrixDataOperable> void assertEqualsMatrix(SparseMatrix<T> amatrix , SparseMatrix<T> bmatrix) {

    if(amatrix.size() != bmatrix.size()) {
      assertMatrix(false , "Sparse Matrix sizes are inequal.") ;
    }

    if(amatrix.getNumCols() != bmatrix.getNumCols() ||
            amatrix.getNumRows() != bmatrix.getNumRows()) {
      assertMatrix(false, "Sparse Matrix dimensions are inequals.");
    }

    SparseMatrixIterator<T> aiter = amatrix.iterator();
    SparseMatrixIterator<T> biter = bmatrix.iterator();

    while(aiter.hasNext()) {
      MatrixDataOperable aop = aiter.next();
      MatrixDataOperable bop = biter.next();

      if(aiter.getRow() != biter.getRow() || aiter.getCol() != biter.getCol() ||
              !(aop.equals(bop))) {
        assertMatrix(false, "Sparse Matrix values are different.") ;
      }
    }
    assertMatrix(true, "Sparse Matrices are equal.") ;
  }

}
